﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using mjRegister.Entity;
using System.Text.RegularExpressions;
using System.Reflection;

namespace mjRegister
{
    public partial class SendGift : Form
    {
        public string sendGiftStr;

        public SendGiftInfo SendGiftInfo = new SendGiftInfo();

        RegexOptions options = ((RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline) | RegexOptions.IgnoreCase);

        public SendGift()
        {
            InitializeComponent();
        }

        private void SendGifg_Load(object sender, EventArgs e)
        {
            string oldgiftfile = Path.Combine(Application.StartupPath, "gift.html");

            if (File.Exists(oldgiftfile))
                File.Delete(oldgiftfile);

            string giftfile = Path.Combine(System.IO.Path.GetTempPath(), "gift.html");
            if (!File.Exists(giftfile) || (File.Exists(giftfile) && File.GetLastWriteTime(giftfile).Day != DateTime.Now.Day))
            {
                Assembly assembly = Assembly.GetExecutingAssembly();
                Stream s = assembly.GetManifestResourceStream(string.Format("{0}.{1}", assembly.GetName().Name, "gift.html"));
                if (s != null)
                {
                    using (StreamReader sr = new StreamReader(s))
                    {
                        using (StreamWriter sw = new StreamWriter(giftfile, false,Encoding.UTF8))
                        {
                            sw.Write(sr.ReadToEnd());
                        }
                    }
                }
            }

            this.webBrowser1.Navigate(giftfile);
            this.webBrowser1.Navigating += new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);
        }

        void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            string url = e.Url.ToString();
            if (url.StartsWith("http://sendgift/"))
            {
                e.Cancel = true;

                sendGiftStr = url.Replace("http://sendgift/?", string.Empty);

                string re = "gid=(?<gid>\\d+)&quiet=(?<quiet>\\d+)&anon=(?<anon>\\d+)&content=(?<content>.*?)&texttype=\r\n(?<texttype>.*?)&randomgifttype=(?<randomgifttype>.*?)&advgiftnum=(?<advgiftnum>\\d+)";

                if (Regex.IsMatch(sendGiftStr, re, options))
                {
                    Match match = Regex.Match(sendGiftStr, re, options);

                    SendGiftInfo = new SendGiftInfo 
                    {
                        gid = int.Parse(match.Result("${gid}")),
                        quiet = int.Parse(match.Result("${quiet}")),
                        anon = int.Parse(match.Result("${anon}")),
                        content = match.Result("${content}"),
                        texttype = match.Result("${texttype}"),
                        randomgifttype = int.Parse(match.Result("${randomgifttype}")),
                        advgiftnum = int.Parse(match.Result("${advgiftnum}"))
                    };
                }
                this.DialogResult = DialogResult.OK;

                this.Close();
            }
        }
    }
}
